package com.obas.control {
   import com.ease.framework.CommandCompletionEvent;
   import com.ease.framework.ViewRegistry;
   import com.obas.event.ScriptOperationEvent;
   import com.obas.event.TestPlanOperationEvent;
   import com.obas.flex.common.util.ResourceUtil;
   import com.obas.model.business.TestPlan;
   import com.obas.model.business.TestPlanItem;
   import com.ease.component.managers.ErrorManager;
   import com.ease.component.util.FireFoxCmdSupport;

   import flash.events.EventDispatcher;
   import flash.external.ExternalInterface;

   import mx.collections.ArrayCollection;
   import mx.utils.URLUtil;

   [Event(name="{com.obas.event.TestPlanOperationEvent.REQUEST_ID}", type="com.obas.event.TestPlanOperationEvent")]
   public class TestPlanPlayer extends EventDispatcher {
      public static const LOADER_HTML:String = "agent/core/autloader.html";

      private var _pageId:String;
      private var _loaderURL:String;

      private var _testPlan:TestPlan;
      private var _runningItems:Array;
      private var _feed:Feed;

      public function TestPlanPlayer() {
         _feed = new Feed();
      }

      public function runTestPlan(testPlan:TestPlan):void {
         _testPlan = testPlan;
         dispatchEvent(new TestPlanOperationEvent([_testPlan.id], TestPlanOperationEvent.GET_TEST_ITEM));
      }

      [ResponseHandler("{com.obas.event.TestPlanOperationEvent.RETURN_ID}")]
      public function onTestPlanOperationEvent(event:CommandCompletionEvent):void {
         if (event.error) {
            ErrorManager.showErrorMessage(ResourceUtil.getErrorString("", event.error));
            return ;
         }

         var request:TestPlanOperationEvent = event.request as TestPlanOperationEvent;
         if (request.actionType == TestPlanOperationEvent.GET_TEST_ITEM) {
            var items:ArrayCollection = event.result as ArrayCollection;
            _runningItems = items.source;
            runTestItems();
         }
      }

      private function runTestItems():void {
         if (_runningItems && _runningItems.length > 0) {
            var item:TestPlanItem = _runningItems.shift();
         }
      }

      private function runTestItem(item:TestPlanItem):void {
         var fullLoaderURL:String = getFullLoaderURL();
         var datetime:Date = new Date();
         _pageId = "IE" + datetime.time;
         FireFoxCmdSupport.runCmd(
            "C:\\\\Windows\\\\system32\\\\mshta.exe",
            [fullLoaderURL + "?debug=true&autURL=" + item.testcaseScript.autEndpoint + "&pageId=" + _pageId]);
         dispatchEvent(new ScriptOperationEvent([_pageId, item.testcaseScript.id], ScriptOperationEvent.RUN_TEST_ITEM));
         _feed.subscribe(_pageId);
      }

      private function getFullLoaderURL():String {
         if (!_loaderURL) {
            var contextPath:String = ExternalInterface.call("BrowserHistory.getURL");
            if (URLUtil.isHttpURL(contextPath)) {
               _loaderURL = URLUtil.getFullURL(contextPath, LOADER_HTML)
            } else {
               _loaderURL = "/" + contextPath + "/" + LOADER_HTML;
            }
         }

         return _loaderURL;
      }

      private static var _instance:TestPlanPlayer = null;

      private static function getInstance():TestPlanPlayer {
         if (_instance == null) {
            _instance = new TestPlanPlayer();
            ViewRegistry.instance.eventBus.addParticipant(_instance, true);
         }

         return _instance;
      }

   }
}